<template>
  <div class="fixed-body">
    <div class="login-main">
      <div class="explain">
        <div class="login-aside-doc">
          <div class="login-aside-title">欢迎来到 万月家沅 订单系统</div>
          <div class="login-aside-desc">万月家沅 订单系统 是一个信息的后台管理系统。采用了前后端分离开发模式。</div>
        </div>
      </div>

      <div class="login-formbox">
        <div class="login-title">订单系统</div>
        <div class="login-sub-title">欢迎来到 订单系统，请登录！</div>
        <el-form id="formLogin"
                 ref="loginForm"
                 :model="loginForm"
                 :rules="loginRules">
          <el-form-item prop="username">
            <el-input type="text"
                      auto-complete="off"
                      placeholder="请输入登录账号"
                      v-model="loginForm.username">
              <el-icon slot="prefix"
                       type="user"
                       :style="{ color: 'rgba(0,0,0,.25)' }" />
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input placeholder="请输入密码"
                      type="password"
                      v-model="loginForm.password"
                      @keyup.enter.native="handleLogin">
              <el-icon slot="prefix"
                       type="lock"
                       :style="{ color: 'rgba(0,0,0,.25)' }" />
            </el-input>
          </el-form-item>
          <!--   取消验证码  后期看需求再加   -->
          <!--      <el-form-item prop="code" v-if="captchaOnOff">-->
          <!--        <el-input-->
          <!--          v-@keyup.enter.native="handleLogin"-->
          <!--        >-->
          <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
          <!--        </el-input>-->
          <!--       model="loginForm.code"-->
          <!--          auto-complete="off"-->
          <!--          placeholder="验证码"-->
          <!--          style="width: 63%"-->
          <!--           <div class="login-code">-->
          <!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
          <!--        </div>-->
          <!--      </el-form-item>-->
          <el-checkbox v-model="loginForm.rememberMe"
                       style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="margin-top: 24px">
            <el-button size="large"
                       type="primary"
                       htmlType="submit"
                       class="login-button"
                       :loading="loading"
                       @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import { getCodeImg } from "@/api/system/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
// import { LoginAsideSvg } from '@/core/icons'

export default {
  name: "Login",

  data () {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created () {
    // this.getCode();
    //this.getCookie();


  },
  methods: {
    //获取验证码
    // getCode () {
    //   getCodeImg().then(res => {
    //     this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
    //     if (this.captchaOnOff) {
    //       this.codeUrl = "data:image/gif;base64," + res.img;
    //       this.loginForm.uuid = res.uuid;
    //     }
    //   });
    // },
    getCookie () {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          console.log("111")
          // this.loading = true;  //转圈圈等待效果
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
            this.$router.go(0);
            console.log("登录成功");

          }).catch(() => {
            this.loading = false;
            // if (this.captchaOnOff) {
            //   this.getCode();
            // }
          });
        }
      });
    }
  }
};
</script>

<style  lang="less" scoped>
.fixed-body {
  width: 100%;
  min-height: 100%;
  height: 100%;
  background: #f0f2f5 url(~@/assets/background.svg) no-repeat 50%;
  background-color: #f0f2f5;
  background-size: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  .login-main {
    height: 560px;
    max-width: 900px;
    width: 100%;
    background: rgba(255, 255, 255, 1);
    border-radius: 0px 4px 4px 0px;
    box-shadow: 0px 20px 40px 0px rgba(0, 0, 0, 0.08);
    transition: 0.2s ease-in-out;
    display: flex;
    flex-direction: row;

    .explain {
      height: 560px;
      text-align: right;
      padding-left: 0;
      padding-right: 0;
      background: linear-gradient(
          314deg,
          rgba(113, 113, 198, 1) 0%,
          rgba(113, 113, 198, 1) 100%
        )
        rgba(113, 113, 198, 0.65);
      background-color: rgba(0, 170, 231, 0.65);
      background-blend-mode: saturation;
      border-radius: 4px 0px 0px 4px;
      box-shadow: 0px 20px 40px 0px rgba(0, 0, 0, 0.08);
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      flex-basis: 55%;
      flex-shrink: 0;

      .login-aside-doc {
        .login-aside-title {
          text-align: center;
          line-height: 48px;
          font-size: 32px;
          font-weight: 400;
          color: rgba(255, 255, 255, 1);
          margin-bottom: 12px;
        }

        .login-aside-desc {
          text-align: center;
          line-height: 18px;
          font-size: 12px;
          font-weight: 400;
          color: rgba(255, 255, 255, 1);
          margin-left: 82px;
          margin-right: 81px;
        }
      }
    }

    .login-formbox {
      flex: 1 auto;
      padding: 80px 20px 20px 20px;

      .login-title {
        color: rgba(113, 113, 198, 1);
        font-size: 32px;
        font-weight: 400;
        line-height: 48px;
        margin-bottom: 12px;
        text-align: center;
      }

      .login-sub-title {
        color: rgba(0, 0, 0, 0.45);
        font-size: 14px;
        font-weight: 400;
        line-height: 22px;
        margin-bottom: 64px;
        text-align: center;
      }

      button.login-button {
        padding: 0 15px;
        font-size: 16px;
        height: 40px;
        width: 100%;
        background-color: rgb(91, 132, 176);
        border: 1px solid rgb(91, 132, 176);
      }
    }
  }
}

@media screen and (max-width: 950px) {
  .fixed-body .login-main {
    width: 560px;

    .explain {
      display: none;
    }
  }
}

@media screen and (max-width: 560px) {
  .fixed-body .login-main {
    width: 95%;
  }
}

@media screen and (max-width: 400px) {
  .fixed-body .login-main {
    width: 100%;
    height: 100%;

    .login-formbox {
      padding-top: 100px;
    }
  }
}
</style>
